﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace ASmile.ORM.ADO
{
    public class SQLHelper : IDBHelper
    {
        public SQLHelper(string connStr)
            : base(connStr)
        { }

        SqlConnection _DBConnectionObj;
        SqlCommand _DbCommandObj;
        SqlDataAdapter _DbDataAdapterObj;

        public override string ParamSign => Common.PARAMSIGN_SQLSERVER;

        public override DbConnection DBConnectionObj
        {
            get
            {
                //SqlBulkCopy aa = new SqlBulkCopy(new SqlConnection());
                if (_DBConnectionObj == null)
                {
                    _DBConnectionObj = new SqlConnection(ConnStr);
                }
                return _DBConnectionObj;
            }
        }

        public override DbCommand DbCommandObj
        {
            get
            {
                if (_DbCommandObj == null)
                {
                    _DbCommandObj = new SqlCommand();
                }
                return _DbCommandObj;
            }
        }

        protected internal override DbDataAdapter DbDataAdapterObj
        {
            get
            {
                if (_DbDataAdapterObj == null)
                {
                    _DbDataAdapterObj = new SqlDataAdapter();
                }
                return _DbDataAdapterObj;
            }
        }

        public override DbParameter CreateDbParameter(string paramName, object paramValue)
        {
            return new SqlParameter(paramName, paramValue);
        }

        /// <summary>
        /// 批量拷贝插入
        /// </summary>
        public override int BulkInsert(DataTable dt)
        {
            //原版本：4.6.1，升级为 4.8.5

            SqlBulkCopy bulkCopy = new SqlBulkCopy(ConnStr, SqlBulkCopyOptions.KeepNulls);
            try
            {
                bulkCopy.DestinationTableName = dt.TableName;
                //bulkCopy.BatchSize = 10; //每次插入的数量，默认是全部
               
                //foreach (DataColumn item in dt.Columns)
                //{
                //    bulkCopy.ColumnMappings.Add(item.ColumnName, item.ColumnName);
                //}

                bulkCopy.WriteToServer(dt);
                return dt.Rows.Count;
            }
            catch (Exception ex)
            {
                var dbEx = new DBHelperException("SqlServer BulkInsert 发生异常", this, ex);
                throw dbEx;
            }
            finally
            {
                bulkCopy.Close();
            }
        }

        ///// <summary>
        ///// 批量拷贝插入
        ///// </summary>
        //public override int BulkInsert(DataTable dt)
        //{
        //    SqlBulkCopy bulkCopy;
        //    if (_DBConnectionObj == null)
        //    {
        //        _DBConnectionObj = new SqlConnection(ConnStr);
        //    }
        //    if (IsTrans)
        //    {
        //        bulkCopy = new SqlBulkCopy(_DBConnectionObj, SqlBulkCopyOptions.Default, DbTransObj as SqlTransaction);
        //    }
        //    else
        //    {
        //        bulkCopy = new SqlBulkCopy(new SqlConnection(ConnStr),
        //            SqlBulkCopyOptions.TableLock, null

        //            );
        //    }
        //    try
        //    {
        //        bulkCopy.DestinationTableName = dt.TableName;
        //        //bulkCopy.BatchSize = 1000; //每次插入的数量，默认是全部
        //        //OpenConnection();

        //        bulkCopy.WriteToServer(dt);
        //        return dt.Rows.Count;
        //    }
        //    catch (Exception ex)
        //    {
        //        var dbEx = new DBHelperException("SqlServer BulkInsert 发生异常", this, ex);
        //        throw dbEx;
        //    }
        //    finally
        //    {
        //        //这里关闭也能正常走事务处理
        //        bulkCopy.Close();
        //        CloseConnect();
        //    }
        //}
    }
}